<?php

namespace App\Http\Controllers\Admin\Goods;

use App\Exceptions\ShopException;
use App\Services\Admin\Goods\GoodsService;
use Exception;
use Illuminate\Http\JsonResponse;

class GoodsController extends BaseGoodsController
{
    protected GoodsService $goodsService;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->goodsService = $this->loadService('Admin\Goods\GoodsService');
    }

    /**
     * 商品列表
     * @return JsonResponse
     * @throws Exception
     */
    public function list(): JsonResponse
    {
        $list = $this->goodsService->list($this->params);
        return outputJsonResult($list);
    }

    /**
     * 商品详情
     * @return JsonResponse
     * @throws ShopException
     */
    public function detail(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $detail = $this->goodsService->detail($this->params);
        return outputJsonResult($detail);
    }

    /**
     * 创建商品
     * @return JsonResponse
     * @throws Exception
     */
    public function create(): JsonResponse
    {
        $rule = [
            'categoryId' => 'required|integer|min:1',
            'name' => 'required|string|min:1',
            'coverImg' => 'required|string|min:1',
            'price' => 'required|string|min:1',
            'quantity' => 'required|integer|min:1',
            'showSalesQuantity' => 'required|integer|min:0',
            'weight' => 'required|string|min:1',
            'shippingFee' => 'required|string|min:1',
        ];
        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->goodsService->create($this->params);
        return outputJsonResult();
    }

    /**
     * 更新商品
     * @return JsonResponse
     * @throws Exception
     */
    public function update(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
            'categoryId' => 'required|integer|min:1',
            'name' => 'required|string|min:1',
            'coverImg' => 'required|string|min:1',
            'price' => 'required|string|min:1',
            'quantity' => 'required|integer|min:1',
            'showSalesQuantity' => 'required|integer|min:0',
            'weight' => 'required|string|min:1',
            'shippingFee' => 'required|string|min:1',
        ];
        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->goodsService->update($this->params);
        return outputJsonResult();
    }

    /**
     * 删除商品
     * @return JsonResponse
     * @throws Exception
     */
    public function delete(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];
        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->goodsService->delete($this->params);
        return outputJsonResult();
    }

    /**
     * 上架/下架
     * @return JsonResponse
     */
    public function release(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
            'status' => 'required|integer|in:0,1',
        ];
        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->goodsService->release($this->params);
        return outputJsonResult();
    }

    /**
     * 推荐/取消推荐
     * @return JsonResponse
     */
    public function recommend(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
            'status' => 'required|integer|in:0,1',
        ];
        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->goodsService->recommend($this->params);
        return outputJsonResult();
    }
}
